﻿namespace Sai.Core.Introspection
{
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.Linq;

	/// <summary>
	/// Used to track container->content relationships.
	/// </summary>
	/// <typeparam name="Container">The container for the data.</typeparam>
	/// <typeparam name="Content">The data contained.</typeparam>
	internal class ContainerContentLookup<Container, Content>
	{
		private Dictionary<Container, List<Content>> dictionary = new Dictionary<Container, List<Content>>();

		public ReadOnlyCollection<Container> Containers
		{
			get
			{
				return new ReadOnlyCollection<Container>(this.dictionary.Keys.ToList());
			}
		}

		public int Count
		{
			get { return this.dictionary.Count; }
		}

		public void Add(Container container, Content content)
		{
			if (!this.dictionary.ContainsKey(container))
			{
				this.dictionary.Add(container, new List<Content>());
			}

			this.dictionary[container].Add(content);
		}

		public bool Contains(Container container)
		{
			return this.dictionary.ContainsKey(container);
		}

		public ReadOnlyCollection<Content> ContentFor(Container container)
		{
			if (this.dictionary.ContainsKey(container))
			{
				return new ReadOnlyCollection<Content>(this.dictionary[container]);
			}

			return new ReadOnlyCollection<Content>(new List<Content>());
		}
	}
}
